<?php
// Web环境下的FFmpeg测试脚本（避免open_basedir限制）

// 设置错误报告级别
error_reporting(E_ALL);
ini_set('display_errors', 1);

// 设置响应头
header('Content-Type: text/plain; charset=utf-8');

echo "=== FFmpeg Web环境测试 ===\n\n";

// 定义FFmpeg路径
$ffmpegPath = '/www/wwwroot/tbbs-api.chinesemir.com/bin/ffmpeg';

// 通过执行命令来检查FFmpeg是否可用，而不是使用file_exists
function isCommandAvailable($commandPath) {
    $command = "$commandPath -version 2>&1";
    exec($command, $output, $returnCode);
    return $returnCode === 0 || $returnCode === 1;
}

// 检查FFmpeg是否可用
echo "1. 检查FFmpeg可用性:\n";
$hasFfmpeg = isCommandAvailable($ffmpegPath);
echo "FFmpeg是否可用: " . ($hasFfmpeg ? '是' : '否') . "\n\n";

if ($hasFfmpeg) {
    // 测试执行FFmpeg命令
    echo "2. 执行FFmpeg命令:\n";
    $cmd = "$ffmpegPath -version 2>&1";
    exec($cmd, $output, $returnCode);
    
    echo "执行命令: $cmd\n";
    echo "返回码: $returnCode\n";
    echo "输出内容:\n" . implode("\n", $output) . "\n\n";
    
    // 测试一个简单的转换命令
    echo "3. 测试简单转换命令:\n";
    $testCmd = "$ffmpegPath -h 2>&1 | head -n 5";
    exec($testCmd, $helpOutput, $helpReturnCode);
    
    echo "执行命令: $testCmd\n";
    echo "返回码: $helpReturnCode\n";
    echo "帮助信息前5行:\n" . implode("\n", $helpOutput) . "\n\n";
} else {
    echo "2. 尝试其他方法检测FFmpeg:\n";
    
    // 尝试使用which命令
    $whichCmd = "which ffmpeg 2>&1";
    exec($whichCmd, $whichOutput, $whichReturnCode);
    echo "执行命令: $whichCmd\n";
    echo "返回码: $whichReturnCode\n";
    echo "输出: " . implode("\n", $whichOutput) . "\n\n";
    
    // 尝试直接执行ffmpeg命令（假设在PATH中）
    $directCmd = "ffmpeg -version 2>&1";
    exec($directCmd, $directOutput, $directReturnCode);
    echo "执行命令: $directCmd\n";
    echo "返回码: $directReturnCode\n";
    if ($directReturnCode === 0 || $directReturnCode === 1) {
        echo "直接执行ffmpeg成功\n";
        echo "输出内容:\n" . implode("\n", $directOutput) . "\n\n";
    } else {
        echo "直接执行ffmpeg失败\n";
        echo "输出内容:\n" . implode("\n", $directOutput) . "\n\n";
    }
}

// 显示环境信息
echo "4. 环境信息:\n";
echo "当前用户: " . get_current_user() . "\n";
echo "PHP运行用户: " . (function_exists('posix_geteuid') ? posix_geteuid() : 'N/A') . "\n";
echo "open_basedir: " . ini_get('open_basedir') . "\n";

echo "\n=== 测试完成 ===\n";